class_name ParticleTankMissle
extends MagicBulletBase

@onready var animation_player: AnimationPlayer = $AnimationPlayer
@onready var search_area: Area2D = $SearchArea

@export var speed: float = 200
var dir: Vector2 = Vector2.RIGHT
var attack_target: Node2D = null

@export var life_time: float = 0.5
var life_timer: float = 0

var is_destroy: bool = false

@onready var shooting_audio: AudioStreamPlayer2D = $AudioStreamPlayer2D
@onready var audio_shoot: AudioStreamPlayer2D = $AudioShoot


func _ready() -> void:
	audio_shoot.play()
	shooting_audio.play()


func _physics_process(delta: float) -> void:
	process_search()
	process_move(delta)
	process_destroy(delta)
	
	if not is_destroy:
		process_hit()
		

func process_hit():
	var areas = hit_area.get_overlapping_areas()
	if areas.size() <= 0: return
	
	death()
	explosion()


func process_search():
	if attack_target: return
	
	var target_areas = search_area.get_overlapping_areas()
	if target_areas.size() <= 0: return
	
	var min_distance: float = INF
	var min_distance_target: Node2D = null
	for area in target_areas:
		var result = GlobalUtils.raycast_to(global_position, area.source.buff_parent.global_position, 1)
		if result: continue
		
		var distance = global_position.distance_to(area.source.buff_parent.global_position)
		if distance < min_distance:
			min_distance = distance
			min_distance_target = area.source
	
	if min_distance_target:
		attack_target = min_distance_target
	

func process_destroy(delta: float):
	if is_destroy: return
	
	life_timer += delta
	if life_timer >= life_time:
		death()
		explosion()
	

func process_move(delta: float):
	if is_destroy: return
	
	if attack_target:
		var target_dir = (attack_target.global_position - global_position).normalized()
		if attack_target is UnitBase:
			target_dir = (attack_target.ray_cast_point.global_position - global_position).normalized()
		dir = dir.lerp(target_dir, 0.05).normalized()
	
	position += dir * speed * delta
	rotation = dir.angle()
	
			
func _on_hit_area_body_entered(body: Node2D) -> void:
	death()
	explosion()
	

func death():
	dir = Vector2.ZERO
	is_destroy = true
	animation_player.play("hide")
	await animation_player.animation_finished
	queue_free()	
	

func explosion():
	shooting_audio.stop()
	
	var inst = (ResourceManager.get_resource("particle_missle_explosion_effect") as PackedScene).instantiate() as ParticleExplosionEffect
	var level = get_tree().current_scene as Level
	level.bullets_parent.add_child(inst)
	inst.set_is_enemy(self.is_enemy)
	inst.global_position = global_position
	inst.sender = sender
	inst.attack = attack
	inst.critical_rate = critical_rate
	inst.critical_bonus = critical_bonus
	inst.knockback = knockback
	inst.hit_cam_shake_strength = hit_cam_shake_strength
	inst.hit_cam_shake_duration = hit_cam_shake_duration
	
	level.camera.start_shake(hit_cam_shake_strength, hit_cam_shake_duration)
	

func set_is_enemy(b: bool):
	super.set_is_enemy(b)
	
	if b:
		search_area.set_collision_mask_value(7, true)
		search_area.set_collision_mask_value(6, false)
	else:
		search_area.set_collision_mask_value(7, false)
		search_area.set_collision_mask_value(6, true)
		
